package com.tanguy.driver.ui.account.activity

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.support.v4.content.ContextCompat
import android.support.v4.widget.DrawerLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.View
import com.tanguy.driver.R
import com.tanguy.driver.base.ImmersionTitleActivity
import com.tanguy.driver.bean.car.CarBrandBean
import com.tanguy.driver.custom.SuspendDecoration
import com.tanguy.driver.custom.WaveSideBar
import com.tanguy.driver.interfaces.OnItemClickListener
import com.tanguy.driver.net.http.HttpManager
import com.tanguy.driver.ui.account.adapter.BatchTypeAdapter
import com.tanguy.driver.ui.account.adapter.CarBrandAdapter
import com.tanguy.driver.utlis.extend.dp2px
import com.tanguy.driver.utlis.extend.requestSucceed
import com.tanguy.driver.utlis.extend.sp2px
import com.tanguy.driver.Const
import org.jetbrains.anko.find
import java.util.*

/**
 * Created by dab on 2017/11/3 0003 16:36
 * 选择车辆类型
 */
class VehicleTypeActivity : ImmersionTitleActivity() {
    override fun setContentViewRes(): Int = R.layout.activity_vehicle_type
    private var mCarBrands: List<CarBrandBean> = java.util.ArrayList()
    private val mRvBrand by lazy { find<RecyclerView>(R.id.rv_brand) }
    private val mRvType by lazy { find<RecyclerView>(R.id.rv_type) }
    private val mDlRoot by lazy { find<DrawerLayout>(R.id.dl_root) }
    private val mSideBar by lazy { find<WaveSideBar>(R.id.side_bar) }
    private val mCarBrandAdapter = CarBrandAdapter()
    private val mBatchTypeAdapter = BatchTypeAdapter()
    private var brandName = ""
    override fun initView() {
        super.initView()
        title = "选择车型"
        titleColor = R.color.textColor
        initRecyclerView()
        mDlRoot.setScrimColor(Color.TRANSPARENT)
        mDlRoot.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
        mDlRoot.addDrawerListener(object : DrawerLayout.SimpleDrawerListener() {
            override fun onDrawerClosed(drawerView: View) {
                super.onDrawerClosed(drawerView)
                mCarBrandAdapter.cancelSelect()
            }
        })
        HttpManager.getBrandList()
                .requestSucceed(this) {
                    mCarBrands = it
                    Collections.sort(mCarBrands)
                    mCarBrandAdapter.setCarBrandBeans(mCarBrands)
                }
        mSideBar.setOnSelectIndexItemListener { index ->
            for (i in mCarBrands.indices) {
                if (mCarBrands[i].initial == index) {
                    (mRvBrand.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(i, 0)
                    return@setOnSelectIndexItemListener
                }
            }
        }

    }

    private fun initRecyclerView() {
        val suspendDecoration = object : SuspendDecoration() {
            override fun isSameGroup(priorGroupId: Int, nowGroupId: Int): Boolean {
                return mCarBrands[priorGroupId].initial == mCarBrands[nowGroupId].initial
            }

            override fun showTitle(position: Int): String {
                return mCarBrands[position].initial
            }
        }
        suspendDecoration.setTitleHeight(dp2px(30))
                .setTitleSize(sp2px(14))
                .setTitleGravity(SuspendDecoration.TitleGravity.TITLE_GRAVITY_LEFT)
                .setBackgroundColor(ContextCompat.getColor(this, R.color.color_f5f5f5))
                .setTextColor(ContextCompat.getColor(this, R.color.color_999999))
                .setTextOffsetX(dp2px(16))
        mRvBrand.layoutManager = LinearLayoutManager(this)
        mRvBrand.adapter = mCarBrandAdapter
        mRvBrand.addItemDecoration(suspendDecoration)
        //类型
        mRvType.layoutManager = LinearLayoutManager(this)

        mRvType.adapter = mBatchTypeAdapter
        mCarBrandAdapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(view: View, position: Int) {
                val carBrandBean = mCarBrands[position]
                val id = carBrandBean.id
                brandName = carBrandBean.name
                getModelList(id)
            }
        })

    }

    private fun getModelList(id: Int) {
        HttpManager.getModelList(id)
                .requestSucceed(this) {
                    mBatchTypeAdapter.setCarTypesBeans(it)
                    mDlRoot.openDrawer(Gravity.END)
                    mBatchTypeAdapter.setDataCallBack({ carTypesBean ->
                        val data = Intent()
                        data.putExtra(Const.IntentCode.VEHICLE_TYPE, carTypesBean.id.toString())
                        data.putExtra(Const.IntentCode.VEHICLE_STRING, brandName + " " + carTypesBean.name)
                        setResult(Activity.RESULT_OK, data)
                        finish()

                    })
                }
    }
}